//
//  RWDBVersionManager.m
//  Chat
//
//  Created by 任伟 on 16/3/8.
//  Copyright © 2016年 任伟. All rights reserved.
//

#import "RWDBVersionManager.h"
#import "DbUtil.h"

//1.1.1版本：用于添加提醒内容的祝福语功能，删除原来数据库
#define OldBaseVersion      @"1.1.1"
#define DBVersionStr        @"dataBaseVersion"
#define DBAppVersion        @"appVersion"

@implementation RWDBVersionManager

/// 数据库版本控制
+ (void)updateDataBaseVersion {

    //    int result1 = [[HERequestHelper appVersion] compare:@"1.0.160108" options:NSLiteralSearch];
    NSString *appVersion = [self appVersion];
    NSUserDefaults *dataBaseDefaults = [NSUserDefaults standardUserDefaults];
    NSString *dataBaseVersion = [dataBaseDefaults objectForKey:DBVersionStr];
//    NSString *oldAppVersion = [dataBaseDefaults objectForKey:DBAppVersion];
    if (![dataBaseVersion isEqualToString:OldBaseVersion]) {
        //        1.0.160108  版本数据库删除操作
        NSFileManager* fileManager=[NSFileManager defaultManager];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
        //文件名
        NSString *uniquePath=[[paths objectAtIndex:0] stringByAppendingPathComponent:@"tft.sqlite"];
        BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:uniquePath];
        if (!blHave) {
            NSLog(@"没有该文件！");
            [dataBaseDefaults setValue:OldBaseVersion forKey:DBVersionStr];
//            [dataBaseDefaults setValue:appVersion forKey:DBAppVersion];
            [self changeColumn];
        }else {
            NSLog(@" have");
            BOOL blDele= [fileManager removeItemAtPath:uniquePath error:nil];
            if (blDele) {
                NSLog(@"删除数据库文件成功！");
                [dataBaseDefaults setValue:OldBaseVersion forKey:DBVersionStr];
//                [dataBaseDefaults setValue:appVersion forKey:DBAppVersion];
                [self changeColumn];
            }else {
                NSLog(@"删除数据库文件失败");
            }
        }
    } else {
        if (!appVersion || ![appVersion isEqualToString:appVersion]) {
            [self changeColumn];
        }
    }
    
    [dataBaseDefaults synchronize];
}

/// 数据库字段修改
+ (void)changeColumn
{
    //倒计时功能数据库操作
    [[DbUtil sharedDatabase] execUpdate:[NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (date text,ringName text,repeat text,repeatCustom text,ahead text,reminderType text,explain text,firstDate text,id text,countDownSec text,runStatus text);",ReminderTableName]];  //创建表格；
    [[DbUtil sharedDatabase] execUpdate:[NSString stringWithFormat:@"ALTER TABLE %@ ADD COLUMN countDownSec text;",ReminderTableName]];  //增加字段：倒计时秒数；
    [[DbUtil sharedDatabase] execUpdate:[NSString stringWithFormat:@"ALTER TABLE %@ ADD COLUMN runStatus text;",ReminderTableName]];    //增加字段：倒计时状态：0、暂停；1、启动；2、停止；
    
}


+ (NSString *)appVersion
{
    NSDictionary *info = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Info" ofType:@"plist"]];
    return [info objectForKey:@"CFBundleShortVersionString"];
}

@end
